package scr 
{
	import flash.events.Event;
	import flash.text.TextField;
	import ru.gosoplata.model.TmpModel;
	import uicontrols.ButtonBase;
	import uicontrols.ButtonSelectService;
	import usr.ServiseData;
	import usr.WCTehnik;
	public class ScreenWCServise extends ScreenBase implements IScreen 
	{
		private var btn_wcl:ButtonBase = new ButtonBase("wcl");
		private var btn_wcr:ButtonBase = new ButtonBase("wcr");
		private var btn_send:ButtonBase = new ButtonBase("wcsend");
		private var btn_exit:ButtonBase = new ButtonBase("exit");
		public var btn_page1:ButtonBase = new ButtonBase("but_page",0x111111);
		public var btn_page2:ButtonBase = new ButtonBase("but_page",0x111111);
		public var btn_page3:ButtonBase = new ButtonBase("but_page",0x111111);
		public var btn_page4:ButtonBase = new ButtonBase("but_page",0x111111);
		public var btn_page5:ButtonBase = new ButtonBase("but_page",0x111111);
		public var btn_page6:ButtonBase = new ButtonBase("but_page",0x111111);
		//private var fmt:TextFormat;
		private var current_page:Number = 1;
		private var planes_cnt:Number = 30;
		private var pic_count:Number=0;
		private var status_txt:TextField;
		private var pages_count:Number = 1;
		
		public function ScreenWCServise() 
		{
			super("wc_servise");
		}
		
		override protected function init(e:Event):void 
		{
			super.init(e);
			
			status_txt = new TextField();
			status_txt.embedFonts = true;
			status_txt.x = 10;
			status_txt.width = 1000;
			status_txt.defaultTextFormat = TmpModel.getTextFormat(16, 0x676767,true);
			status_txt.visible = false;
			status_txt.text = "WcStatus = false";
			addChild(status_txt);
			createButtons();
			current_page = 1;
		}
		
		private function createPlanes():void {
			var chplane:ButtonSelectService;
			var hg:Number = 5;// vertical clearence
			for (var ii:Number = 1; ii <= planes_cnt; ii++ ) {
				chplane = new ButtonSelectService();
				var strname:String = ("chplane" + ii);
				chplane.name = strname;
				chplane.x = 700;// btn_wcr.x + btn_wcr.width + 200;
				var num:Number = ii % 5;
				if (num == 0) num = 5;
				chplane.y = btn_wcr.y +50+ (110 + hg) * num;
				chplane.visible = false;
				addChild(chplane);
			}
		}
		
		override public function reDraw():void {
			var teh:WCTehnik = ServiseData.getInstance().currteh;
			pages_count = Math.ceil(teh._data.length / 5);
			btn_page1.visible = (pages_count >= 1);
			btn_page2.visible = (pages_count >= 2);
			btn_page3.visible = (pages_count >= 3);
			btn_page4.visible = (pages_count >= 4);
			btn_page5.visible = (pages_count >= 5);
			btn_page6.visible = (pages_count >= 6);
			btn_page1.fixDownBtn();
			//draw info planes
			for (var ii:Number = 1; ii <= 30; ii++ ) {
				var chplane:ButtonSelectService = (getChildByName("chplane" + ii) as ButtonSelectService);
				chplane.swCheck(false);
				if (ii <= teh._data.length && ii<=5) {
					chplane.visible = true;
					chplane.info = teh._data[ii - 1][1];
				}else {
					chplane.visible = false;
				}
			}
			btn_wcl.visible = chekDoorVis(teh);
			btn_wcr.visible = chekDoorVis(teh);
		}
		
		private function chekDoorVis(teh:WCTehnik):Boolean {
			return !teh.checkRole(3);
		}
		
		private function createButtons():void {
			pic_count = 0;
			btn_wcl.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_wcl.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			btn_wcl.visible = false;
			addChild(btn_wcl);
			pic_count++;
			btn_wcr.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_wcr.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			btn_wcr.visible = false;
			addChild(btn_wcr);
			pic_count++;
			btn_send.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_send.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			addChild(btn_send);
			pic_count++;
			btn_exit.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_exit.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			addChild(btn_exit);
			pic_count++;
			for (var ii:Number = 1; ii <= 6; ii++ ) {
				this["btn_page"+ii].addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
				this["btn_page"+ii].addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
				this["btn_page" + ii].btn_index = ii;
				addChild(this["btn_page" + ii]);
				pic_count++;
			}
			createPlanes();
			
		}
		//private var sscale:Number = 0.7;
		public function allPicLoaded(e:Event):void 
		{
			pic_count--;
			if (pic_count == 0) {
				status_txt.y = 180;
				btn_wcr.x = 150;
				btn_wcr.y = 200;
				btn_wcl.x = btn_wcr.x;
				btn_wcl.y = btn_wcr.y + btn_wcr.height +20;
				btn_send.x = btn_wcr.x;
				btn_send.y = btn_wcl.y + btn_wcl.height + btn_wcl.height +20;
				btn_exit.x = stage.stageWidth - btn_exit.width - 20;
				btn_exit.y = stage.stageHeight - btn_exit.height - 20;

				var hg:Number = 10;

				
				for (var ii:Number = 1; ii <= 6; ii++ ) {
					this["btn_page" + ii].y = btn_exit.y - 100 - 20;
					this["btn_page" + ii].x = 750 + (ii - 1) *(this["btn_page" + ii].width + 5);
					this["btn_page" + ii].btn_index = ii;
					(this["btn_page" + ii] as ButtonBase).addTextIcon(ii.toString());
				}
				loadBanner(TmpModel.IMG_FOLDER + "/service/teh_title.png");
			}
		}	
		
		private function sendDataToServer():void {
			var teh:WCTehnik = ServiseData.getInstance().currteh;
			var str:String = ""+teh._id.toString() + "&WcWorks=";
			var wa:Array = new Array();
			for (var ii:Number = 1; ii <= teh._data.length; ii++ ) {
				//push teh data to request
				if ((getChildByName("chplane" + ii) as ButtonSelectService).isChecked()) wa.push(teh._data[ii - 1][0]);
			}
			str += wa.join("|");
			str += "&WcVisit=true";
			TmpModel.acceptor.wcServicer(str);
		}
		private function showPageNum(num:Number):void {
			var teh:WCTehnik = ServiseData.getInstance().currteh;
				for (var ii:Number = 1; ii <= 6; ii++ ) {
					(this["btn_page" + ii] as ButtonBase).unfixBtn();
				}
				(this["btn_page" + num] as ButtonBase).fixDownBtn();
				current_page = num;
			for (ii = 1; ii < 30; ii++ ) {
				//redraw info txts
				if (ii < current_page * 5 + 1 && ii > (current_page-1) * 5 && ii <= teh._data.length) {
					var chplane:ButtonSelectService = (getChildByName("chplane" + ii) as ButtonSelectService);
					chplane.visible = true;
					chplane.info = teh._data[ii - 1][1];
				}else {
					(getChildByName("chplane" + ii) as ButtonSelectService).visible = false;
				}
			}

		}
		//TODO: ПЦ
		private function pressBtn(e:Event):void 
		{
			switch(e.target.name) {
				case "but_page":
					showPageNum((e.target as ButtonBase).btn_index);
					break;
				case "wcl":
					TmpModel.acceptor.wcOpen(1);
					break;
				case "wcr":
					TmpModel.acceptor.wcOpen(2);
					break;
				case "wcsend":
					TmpModel.acceptor.wcInit();
					sendDataToServer();
					TmpModel.tmpGlobalController.onClick('info');
					break;
				case "exit":
					TmpModel.tmpGlobalController.onClick('menu');
					break;
			}			
		}		
		
	}

}